# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 -------------------------------------------------
    File Name:     conf_models.py
    Description:   配置模型
 -------------------------------------------------
 """
import os

from pydantic import BaseModel, Field, field_validator
from pydantic_settings import BaseSettings

class ProjectConfig(BaseModel):
    name: str | None = Field(default="", description="项目名称")


class RedisConfig(BaseSettings):
    """Redis配置模型"""
    host: str | None = Field(..., description="redis数据库主机地址")
    port: int | None = Field(default=6379, description="redis端口")
    password: str | None = Field(default="", description="redis密码")
    database: int | None = Field(default=0, description="redis数据库编号")
    timeout: int | None = Field(default=10, description="连接超时时间")

    class Config:
        env_prefix = "REDIS_"





class LogConfig(BaseModel):
    """Log配置模型"""
    log_save_days: int = Field(default=5, description="日志保存天数")
    log_level: str = Field(default="DEBUG", description="最低日志输出级别")
    log_level_console: str = Field(default="INFO", description="控制台最低日志输出级别")
    log_format: str = Field(default="%(asctime)s %(levelname)-8s %(message)s", description="日志输出格式")
    date_format: str = Field(default="%Y-%m-%d %H:%M:%S", description="日期格式")


class EmailConfig(BaseModel):
    """Email配置模型"""
    smtp_server: str = Field(..., description="邮件服务器地址")
    smtp_port: int = Field(default=25, description="邮件服务器端口")
    sender: str = Field(..., description="发送者邮箱地址")
    password: str = Field(..., description="发送者邮箱密码")
    receiver: list[str] = Field([], description="接收者邮箱列表")
    ssl: bool = Field(default=True, description="是否使用SSL加密")
    send: bool = Field(default=False, description="是否开启发送")


class DingDingConfig(BaseModel):
    """钉钉配置模型"""
    webhook: str = Field(..., description="钉钉机器人WebHook地址")
    proxies_http: str = Field(default="", description="代理地址")
    proxies_https: str = Field(default="", description="代理地址")
    send: bool = Field(default=False, description="是否开启发送")


class FeiShuConfig(BaseModel):
    """飞书配置模型"""
    webhook: str = Field(..., description="飞书机器人WebHook地址")
    proxies_http: str = Field(default="", description="代理地址")
    proxies_https: str = Field(default="", description="代理地址")
    send: bool = Field(default=False, description="是否开启发送")


class WeChatConfig(BaseModel):
    """微信配置模型"""
    webhook: str = Field(..., description="微信机器人WebHook地址")
    proxies_http: str = Field(default="", description="代理地址")
    proxies_https: str = Field(default="", description="代理地址")
    send: bool = Field(default=False, description="是否开启发送")




# class AppConfig(BaseSettings):
#     mysql: MySqlConfig | None = None
#
#     class Config:
#         # env_prefix = "MYSQL_"
#         toml_file = "dev.toml"

class MySqlConfig(BaseSettings):
    """MySQL配置模型"""
    host: str | None = Field(default=None, description="Mysql数据库主机地址")
    port: int | None = Field(default=3306, description="数据库端口")
    username: str | None = Field(default="root", description="数据库用户名")
    password: str | None = Field(default="123456", description="数据库密码")
    database: str | None = Field(default=None, description="数据库名称")

    class Config:
        env_prefix = "MYSQL_"
        toml_file = "dev.toml"  # 这种方式不会被设置为环境变量
 # = Field(..., description="数据库配置")


    # @field_validator("*", mode="after")
    # @classmethod
    # def validate_db_connection(cls, v, info):
    #     try:
    #         import pymysql
    #         conn = pymysql.connect(
    #             host=info.data.get('host'),
    #             port=info.data.get('port'),
    #             user=info.data.get('username'),
    #             password=info.data.get('password'),
    #             database=info.data.get('database')
    #         )
    #         conn.close()    # 验证后立即关闭
    #
    #     except Exception as e:
    #         raise ValueError(f"数据库连接验证失败: {e}")
    #     else:
    #         print('数据库连接成功')
    #         return v


if __name__ == '__main__':
    config = MySqlConfig()
